/**
 * Created by Alexander on 02.02.2015.
 */
$(document).ready(function () {
        // get some info about the besitzer
        if (get_cookie("username")==null)
        {
            window.location.replace('MainTestwindow.html');
        }

        uname=document.getElementById("uname");
        uname.innerHTML="Hallo "+get_cookie("username");
        uscore=document.getElementById("uscore");
        uscore.innerHTML="RichtigePunkte "+get_cookie("score");

        //$("#uscore").innerHTML=get_cookie("username");
        //$("#ufalsh").innerHTML=get_cookie("score");
        $.get('data.xml', null, function (data, textStatus) {
            xml = data;
            var ger = new Array();
            var russ = new Array();
            $(xml).find('expression').each(function () {
                var item = $(this);
                ger.push(item.find('deutsch').text());
                russ.push(item.find('russisch').text());
            });

            var el1 = document.createElement("h2");
            var ran;
            ran = Math.floor(Math.random() * ger.length + 1);
            doc = document.createElement("h2");
            doc.innerHTML = ger[ran];
            document.getElementById('frage').appendChild(doc);
            antw = document.getElementById("antworten")
            ran4 = Math.floor(Math.random() * 4);
            for (i = 0; i < 4; i++) {
                myran = Math.floor(Math.random() * ger.length + 1);
                newantw = document.createElement("input");
                newantw.setAttribute("type", "checkbox");
                newantw.setAttribute("id", "antw" + i);
                newantwtext = document.createElement("h3");
                if (i != ran4)
                    newantwtext.innerHTML = russ[myran];
                else    newantwtext.innerHTML = russ[ran];
                antw.appendChild(newantw);
                antw.appendChild(newantwtext);
                antw.appendChild(document.createElement("br"));
            }
        });

        $('input[type=button]').click(function () {
                var checkednum = 0;
                var checkedid = 0;
                $('input[type=checkbox]').each(function () {
                    var item = $(this);
                    if ($(item).is(':checked')==true)
                    {
                        checkednum++;
                        idItem=$(item).attr("id");
                        checkedid=idItem.substr(idItem.length-1);
                    }

                });
                if (checkedid==ran4)
                {
                    alert("Sie haben korrekt die Frage beantwortet");
                    expires=new Date();
                    expires.setTime(expires.getTime() + (1000 * 60 * 10));
                    curr_score=get_cookie("score");
                    cscore=parseInt(curr_score);
                    cscore+=1;
                    set_cookie("score", cscore.toString(), expires);
                    window.location.replace('newtest.html');
                }
                else
                {
                    alert("Sie haben falsch davon");
                }
            }
        );


    }
)


function set_cookie(name, value, expires)
{
    if (!expires)
    {
        expires = new Date();
    }
    document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
}


function get_cookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset)
            if (end == -1) {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return(setStr);
}